package com.team.sion.service.Impl;

import com.team.sion.mapper.SingerMapper;
import com.team.sion.mapper.SongMapper;
import com.team.sion.pojo.*;
import com.team.sion.pojo.Singer;
import com.team.sion.pojo.SingerExample;
import com.team.sion.pojo.Song;
import com.team.sion.pojo.SongExample;
import com.team.sion.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SongServiceImpl implements SongService {
    @Autowired
    private SongMapper songMapper;
    @Autowired
    private SingerMapper singerMapper;

    @Override
    public List<Song> findAll() {
        SongExample example = new SongExample();
        return songMapper.selectByExample(example);
    }

    @Override
    public boolean updateSong(Song song) {
        int i = songMapper.updateByPrimaryKeySelective(song);
        if(i == 1){
            return true;
        }
        return false;
    }

    @Override
    public List<Song> findById(Integer id) {
        SongExample example = new SongExample();
        example.createCriteria().andIdEqualTo(id);
        return songMapper.selectByExample(example);
    }

    @Override
    public List<Song> findBySingerId(Integer singerId) {
        SongExample example = new SongExample();
        example.createCriteria().andSingerIdEqualTo(singerId);
        return songMapper.selectByExample(example);
    }

    @Override
    public List<Song> findByName(String name) {//多表查询 嵌套查询
//        SingerExample example = new SingerExample();
//        example.createCriteria().andNameLike("%"+name+"%");
//        List<Singer> singers = singerMapper.selectByExample(example);
//        List<Integer> arr = new ArrayList<>();
//        for(Singer singer:singers){
//            arr.add(singer.getId());
//        }
//        SongExample songExample = new SongExample();
//        songExample.createCriteria().andSingerIdIn(arr);
//        List<Song> list = songMapper.findBySingerName(name);
//        return songMapper.selectByExample(songExample);
        SongExample example = new SongExample();
        example.createCriteria().andNameLike("%"+name+"%");
        List<Song> songs = songMapper.selectByExample(example);
        List<Song> list = songMapper.findBySingerName(name);
        for(Song song :list){
            if(!songs.contains(song)){
                songs.add(song);
            }
        }
        return songs;
    }

    @Override
    public List<Song> findBySSName(Integer sid, String songName) {
        SongExample example = new SongExample();
        example.createCriteria().andNameEqualTo(songName).andSingerIdEqualTo(sid);
        List<Song> songList = songMapper.selectByExample(example);
        return songList;
    }

    @Override
    public boolean addSong(Song song,String path) {
        SongExample example = new SongExample();
        example.createCriteria().andNameEqualTo(song.getName());
        List<Song> list = songMapper.selectByExample(example);
        song.setUrl(path);
        if(list.size() == 0){
            songMapper.insertSelective(song);
            return true;
        }
        return false;
    }

    @Override
    public void updatePic(Integer id, String url) {
        Song song = songMapper.selectByPrimaryKey(id);
        song.setPic(url);
        song.setId(id);
        songMapper.updateByPrimaryKey(song);
    }
}
